/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.data.enty;

import com.toedter.calendar.JDateChooser;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Blasius
 */
public class PengaturSuratMasuk {

    public static void simpanSurat(String patch, JTextField noSurat, JTextField perihal, JDateChooser tglSurat,
            JDateChooser tglTerima, JTextField pengirim, JTextField penerima) {
        try {
            SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
            String tglSrt = sd1.format(tglSurat.getDate());
            String tglTrm = sd2.format(tglTerima.getDate());
            SuratMasuk sm = new SuratMasuk();
            sm.setNoSurat(noSurat.getText());
            sm.setPerihal(perihal.getText());
            sm.setTanggalSurat(java.sql.Date.valueOf(tglSrt));
            sm.setTanggalTerima(java.sql.Date.valueOf(tglTrm));
            sm.setPengirim(pengirim.getText());
            sm.setPenerima(penerima.getText());
            SuratMasuk.simpanDataBase(sm);
            simpanToFile(patch, sm);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Koneksi Gagal. Coba kembali", "Peringatan", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(PengaturSuratMasuk.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void simpanToFile(String fileLocation, SuratMasuk sm) {
        Writer output = null;
        try {
            String isi = sm.getNoSurat() + ":" + sm.getPerihal() + ":" + sm.getTanggalSurat() + ":\n" + sm.getTanggalTerima() + ":" + sm.getPengirim() + ":" + sm.getPenerima();
            File fl = new File(fileLocation);
            output = new BufferedWriter(new FileWriter(fl));
            output.write(isi);
            output.close();
            JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan dalam file....");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Gagal Menyimpan File....", "Peringatan", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(PengaturSuratMasuk.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                Logger.getLogger(PengaturSuratMasuk.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public static List<SuratMasuk> getDaftarSuratMasuk() {
        List<SuratMasuk> isi = new ArrayList<SuratMasuk>();
        try {
            ResultSet rs = SuratMasuk.getListSuratMasuk();
            while (rs.next()) {
                SuratMasuk sm = new SuratMasuk();
                sm.setNoSurat(rs.getString(1));
                sm.setPerihal(rs.getString(2));
                sm.setTanggalSurat(rs.getDate(3));
                sm.setTanggalTerima(rs.getDate(4));
                sm.setPengirim(rs.getString(5));
                sm.setPenerima(rs.getString(6));
                isi.add(sm);
            }
            return isi;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error menampilkan daftar surat", "Peringatan", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(PengaturSuratMasuk.class.getName()).log(Level.SEVERE, null, ex);
            return isi;
        }
    }

        public static List<SuratMasuk> getDaftarCariSuratMasuk(JDateChooser cDate) {
        List<SuratMasuk> isi = new ArrayList<SuratMasuk>();
        SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd");
        String tglSrt = sd1.format(cDate.getDate());
        try {
            ResultSet rs = SuratMasuk.getListCariSuratMasuk(tglSrt);
            while (rs.next()) {
                SuratMasuk sm = new SuratMasuk();
                sm.setNoSurat(rs.getString(1));
                sm.setPerihal(rs.getString(2));
                sm.setTanggalSurat(rs.getDate(3));
                sm.setTanggalTerima(rs.getDate(4));
                sm.setPengirim(rs.getString(5));
                sm.setPenerima(rs.getString(6));
                isi.add(sm);
            }
            return isi;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error menampilkan hasil pencarian...", "Peringatan", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(PengaturSuratMasuk.class.getName()).log(Level.SEVERE, null, ex);
            return isi;
        }
    }
}
